package com.vm.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.vm.adapter.FragAdapter;
import com.vm.adapter.ImageAdapter;
import com.vm.commons.CurrentGoods;
import com.vm.commons.VMConst;
import com.vm.db.GoodsCollectionDao;
import com.vm.db.ShoppingCartDao;
import com.vm.fragment.GoodsFragment;
import com.vm.model.Goods;
import com.vm.model.GoodsCollection;
import com.vm.model.ShoppingCart;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyVolley;
import com.vm.widget.GuideGallery;
import com.wwtscapp.apk.R;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class GoodsDetailsActivity extends FragmentActivity {
	List<Fragment> fragments;
	public HashMap<String, Bitmap> imagesCache = new HashMap<String, Bitmap>();// 图片缓存
	private ViewPager vp;
	private FragAdapter adapter;
	RadioGroup nav_radiogroup;
	ImageView imgbtn_back;
	// ImageButton imgbtn_shopping_cart;
	ImageView ingbtn_collection;
	ImageView ingbtn_collection1;
	ImageView imgbtn_shopping_cart;
	public List<String> urls;
	public GuideGallery images_ga;
	private static int positon = 0;
	public boolean timeFlag = true;
	Uri uri;
	Intent intent;
	int gallerypisition = 0;
	String goodsId;
	String imageUrl;
	private RequestQueue mQueue;
	private GoodsCollectionDao gDao;
	private GoodsCollection goodsCollection;
	private ShoppingCartDao sDao;
	private ShoppingCart shoppingCart;
	List<ShoppingCart> list2;
	List<GoodsCollection> list;
	int a = 0;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.goods_details_nav);
		mQueue = MyVolley.getRequestQueue();
		// String url = VMConst.REQUEST_URL + "goodsdetail?goodsId=" + goodsId;
		// JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url,
		// null,
		// new Listener<JSONObject>() {
		//
		// @Override
		// public void onResponse(JSONObject arg0) {
		// // TODO Auto-generated method stub
		// Goods goods = JsonUtils.jsonToBean(arg0, Goods.class);
		// imageUrl=goods.getImageUrl();
		// }
		//
		// },null);
		// mQueue.add(jsonObjectRequest);
		gDao = new GoodsCollectionDao(GoodsDetailsActivity.this);
		goodsCollection = new GoodsCollection();
		sDao = new ShoppingCartDao(GoodsDetailsActivity.this);
		shoppingCart = new ShoppingCart();
		imageUrl = getIntent().getStringExtra("imageUrl");
		goodsId = getIntent().getStringExtra("goodsId");
		a = getIntent().getIntExtra("return", 0);
		init();
		initData();
	}

	private void initData() {
		fragments = new ArrayList<Fragment>();
		GoodsFragment goodsFragment = new GoodsFragment();
		fragments.add(goodsFragment);
		vp = (ViewPager) findViewById(R.id.viewpager);
		adapter = new FragAdapter(getSupportFragmentManager(), fragments);
		Bundle args = new Bundle();
		goodsId = getIntent().getStringExtra("goodsId");
		args.putString("goodsId", goodsId);
		goodsFragment.setArguments(args);
		vp.setAdapter(adapter);
		vp.setOffscreenPageLimit(3);
		vp.setCurrentItem(0);
	}

	private void init() {
		Bitmap image = BitmapFactory.decodeResource(getResources(),
				R.drawable.logo1);
		imagesCache.put("background_non_load", image); // 设置缓存中默认的图片
		ingbtn_collection = (ImageView) findViewById(R.id.ingbtn_collection);
		ingbtn_collection1 = (ImageView) findViewById(R.id.ingbtn_collection1);
		imgbtn_back = (ImageView) findViewById(R.id.imgbtn_back);
		imgbtn_back.setOnClickListener(btn_backListener);
		nav_radiogroup = (RadioGroup) findViewById(R.id.nav_radiogroup);
		nav_radiogroup.setOnCheckedChangeListener(group_listener);
		images_ga = (GuideGallery) findViewById(R.id.iv_goodsImage);
		images_ga.setImageActivity(this);
		String[] urls = { imageUrl, imageUrl, imageUrl, imageUrl, imageUrl };
		ImageAdapter imageAdapter = new ImageAdapter(this, urls);
		images_ga.setAdapter(imageAdapter);
		LinearLayout pointLinear = (LinearLayout) findViewById(R.id.gallery_point_linear);
		pointLinear.setBackgroundColor(Color.TRANSPARENT);
		for (int i = 0; i < urls.length; i++) {
			ImageView pointView = new ImageView(this);
			if (i == 0) {
				pointView.setBackgroundResource(R.drawable.feature_point_cur);
			} else
				pointView.setBackgroundResource(R.drawable.feature_point);
			pointLinear.addView(pointView);
		}
		images_ga.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				System.out.println(arg2 + "arg2");
			}
		});
		ingbtn_collection.setOnClickListener(collection_Listener);
		ingbtn_collection1.setOnClickListener(collection_Listener);
		if (gDao.ifExits(goodsId)) {
			ingbtn_collection.setVisibility(View.GONE);
			ingbtn_collection1.setVisibility(View.VISIBLE);
		}
	}

	OnClickListener shoppingCartListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (sDao.ifExits(goodsId)) {
				Toast toast = Toast.makeText(GoodsDetailsActivity.this,
						"该商品已经加入购物车了哦", Toast.LENGTH_SHORT);
				toast.show();
			} else {
				String url = VMConst.REQUEST_URL + "goodsdetail?goodsId="
						+ goodsId;
				JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
						url, null, new Listener<JSONObject>() {

							@Override
							public void onResponse(JSONObject arg0) {
								Goods goods = JsonUtils.jsonToBean(arg0,
										Goods.class);
								shoppingCart.setGoodsId(goodsId);
								shoppingCart.setImageUrl(goods.getImageUrl());
								shoppingCart.setName(goods.getName());
								shoppingCart.setPrice(goods.getPrice());
								shoppingCart.setNumber(1);
								sDao.insert(shoppingCart);
								Toast toast = Toast.makeText(
										GoodsDetailsActivity.this, "加入购物车成功",
										Toast.LENGTH_SHORT);
								toast.show();
							}
						}, null);

				mQueue.add(jsonObjectRequest);
			}
		}

	};
	OnClickListener collection_Listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.ingbtn_collection:
				goodsCollection.setImageUrl(CurrentGoods.GOODSIMG);
				goodsCollection.setGoodsId(CurrentGoods.GOODSID);
				goodsCollection.setName(CurrentGoods.GOODSNAME);
				goodsCollection.setPrice(CurrentGoods.GODOSPRICE);
				gDao.insert(goodsCollection);
				list = gDao.selectall();
				Toast toast = Toast.makeText(GoodsDetailsActivity.this, "收藏成功",
						Toast.LENGTH_SHORT);
				toast.show();
				ingbtn_collection.setVisibility(View.GONE);
				ingbtn_collection1.setVisibility(View.VISIBLE);
				break;
			case R.id.ingbtn_collection1:
				gDao.delete(CurrentGoods.GOODSID);
				Toast toast1 = Toast.makeText(GoodsDetailsActivity.this,
						"取消收藏", Toast.LENGTH_SHORT);
				toast1.show();
				ingbtn_collection1.setVisibility(View.GONE);
				ingbtn_collection.setVisibility(View.VISIBLE);
				break;
			default:
				break;
			}
		}

	};
	public OnCheckedChangeListener group_listener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			switch (checkedId) {
			case R.id.rbtn_introduce:
				vp.setCurrentItem(0);
				break;
			default:
				break;
			}
		}

	};

	public void changePointView(int cur) {
		LinearLayout pointLinear = (LinearLayout) findViewById(R.id.gallery_point_linear);
		View view = pointLinear.getChildAt(positon);
		View curView = pointLinear.getChildAt(cur);
		if (view != null && curView != null) {
			ImageView pointView = (ImageView) view;
			ImageView curPointView = (ImageView) curView;
			pointView.setBackgroundResource(R.drawable.feature_point);
			curPointView.setBackgroundResource(R.drawable.feature_point_cur);
			positon = cur;
		}
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		timeFlag = false;
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// Log.i(TAG, "imgbtn_search_back返回按钮监听");
			if (a == 1) {
				Intent intent = new Intent(GoodsDetailsActivity.this,
						GoodsCollectionActivity.class);
				startActivity(intent);
				finish();
			} else {
				onBackPressed();
			}

		}
	};

	/**
	 * 返回按钮监听(手机自带返回按钮)
	 */
	@Override
	public void onBackPressed() {
		// Log.i(TAG, "返回按钮监听(手机自带返回按钮)");

		if (a == 1) {
			Intent intent = new Intent(GoodsDetailsActivity.this,
					GoodsCollectionActivity.class);
			startActivity(intent);
			finish();
		} else {
			super.onBackPressed();
		}
		// System.out.println("按下了back键   onBackPressed()");
	}
}